package alo.p3.work;

import java.util.EmptyStackException;
//实现栈
public class LLStack {

    private ListNode headNode;
    public LLStack(){
        this.headNode=new ListNode(null);
    }
    //push
    public void push(int data){
        if (headNode==null){
            headNode=new ListNode(data);
        }else if (headNode.getData()==null){
            headNode.setData(data);
        }else {
            ListNode llNode=new ListNode(data);
            llNode.setNext(headNode);
            headNode=llNode;
        }
    }
    //top
    public Integer top(){
        if (headNode==null){
            return null;
        }else return headNode.getData();
    }

    //pop
    public int pop(){
        if (headNode==null){
            throw new EmptyStackException();
        }else {
            int data=headNode.getData();
            headNode=headNode.getNext();
            return data;
        }
    }

    //isEmpty
    public boolean isEmpty(){
        if (headNode==null) return true;
        else return false;
    }

    //deleteStack
    public void deleteStack(){
        headNode =null;
    }

    class ListNode {
        private int data;
        private ListNode next;

        public ListNode(Integer data) {
            this.data = data;
        }


        public Integer getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }


    }
}
